package com.ktdaddy.json;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.ktdaddy.exception.JSONException;

import java.util.Optional;

/**
 * @author kdaddy@163.com
 * @date 2021/7/15 23:00
 */
public class JSON {

    private static final ObjectMapper OBJECT_MAPPER;

    public JSON() {
    }

    public static String toJSONString(Object object) {
        return toJSONString(object, false);
    }

    public static String toJSONString(Object object, boolean prettyFormat) {
        try {
            return prettyFormat ? OBJECT_MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(object) : OBJECT_MAPPER.writeValueAsString(object);
        } catch (Throwable var3) {
            throw new JSONException(var3);
        }
    }

    public static <T> T parse(String text, Class<T> clazz) {
        if (text != null && !text.isEmpty()) {
            try {
                return OBJECT_MAPPER.readValue(text, clazz);
            } catch (Throwable var3) {
                throw new JSONException(var3);
            }
        } else {
            return null;
        }
    }

    public static <T> T parse(String text, TypeReference<T> typeReference) {
        if (text != null && !text.isEmpty()) {
            try {
                return OBJECT_MAPPER.readValue(text, typeReference);
            } catch (Throwable var3) {
                throw new JSONException(var3);
            }
        } else {
            return null;
        }
    }

    public static <T> T parse(String text, JavaType javaType) {
        if (text != null && !text.isEmpty()) {
            try {
                return OBJECT_MAPPER.readValue(text, javaType);
            } catch (Throwable var3) {
                throw new JSONException(var3);
            }
        } else {
            return null;
        }
    }

    public static JsonNode parse(String text) {
        if (text != null && !text.isEmpty()) {
            try {
                return OBJECT_MAPPER.readTree(text);
            } catch (Throwable var2) {
                throw new JSONException(var2);
            }
        } else {
            return null;
        }
    }

    public static <T> T toJavaObject(JsonNode jsonNode, Class<T> tClass) {
        try {
            JsonParser jsonParser = OBJECT_MAPPER.treeAsTokens(jsonNode);
            return OBJECT_MAPPER.readValue(jsonParser, tClass);
        } catch (Throwable var3) {
            throw new JSONException(var3);
        }
    }

    public static <T> T toJavaObject(JsonNode jsonNode, TypeReference<T> typeReference) {
        try {
            JsonParser jsonParser = OBJECT_MAPPER.treeAsTokens(jsonNode);
            return OBJECT_MAPPER.readValue(jsonParser, typeReference);
        } catch (Throwable var3) {
            throw new JSONException(var3);
        }
    }

    public static TypeFactory getTypeFactory() {
        return OBJECT_MAPPER.getTypeFactory();
    }

    public static <T> T toJavaObject(JsonNode jsonNode, JavaType javaType) {
        try {
            JsonParser jsonParser = OBJECT_MAPPER.treeAsTokens(jsonNode);
            return OBJECT_MAPPER.readValue(jsonParser, javaType);
        } catch (Throwable var3) {
            throw new JSONException(var3);
        }
    }

    public static Optional<JsonNode> of(JsonNode jsonNode) {
        return jsonNode != null && !jsonNode.isNull() ? Optional.of(jsonNode) : Optional.empty();
    }

    static {
        OBJECT_MAPPER = (new ObjectMapper()).setSerializationInclusion(JsonInclude.Include.NON_NULL).configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    }
}
